// QRcode.js
var QR = require("../../common/qrcode.js");

Page({

  /**
   * 页面的初始数据
   */
  data: {
    maskHidden: true,
    adress: false,
    placeholder: 'http://www.hn96520.com/'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      distance: options.code
    });

    // 设置屏幕亮度
    if (wx.setScreenBrightness) {
      wx.setScreenBrightness({
        value: 1
      })
    }
  },

  // 二维码
  onReady: function () {
    var size = this.setCanvasSize();
    var initUrl = this.data.placeholder;

    this.createQrCode(this.data.distance, "mycanvas", size.w, size.h);
  },

  //适配不同屏幕大小的canvas
  setCanvasSize: function () {
    var size = {};
    try {
      var res = wx.getSystemInfoSync();
      var scale = 150 / 75;
      var width = res.windowWidth / scale;
      var height = width;
      size.w = width;
      size.h = height;
    } catch (e) {
      console.log("获取设备信息失败" + e);
    }
    return size;
  },
  createQrCode: function (url, canvasId, cavW, cavH) {
    QR.qrApi.draw(this.data.distance, canvasId, cavW, cavH);
  },
  qrcode: function (code) {
    var that = this;
    var size = that.setCanvasSize();
    //绘制二维码
    that.createQrCode(code, "mycanvas", size.w, size.h);
    that.setData({
      maskHidden: true
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.qrcode(this.data.distance);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})